#include "kore2.h"
#include <iostream>
#include <fstream>
#include <istream>
#include <ostream>

/**
 * @brief ConfigFile Constructor, loads a configuration file.
 *
 * Gives full access to a configuration file including reading and writing.
 *
 * @todo Rewrite, value should span to the end of the line.
 * @param std::string The file to load
 */
kore::ConfigFile::ConfigFile(std::string filename)
{
	this->m_configfile = filename;
	printf("[config] Reading %s...\n", filename.c_str());
    char _buff[1024], _ch=' ', tag[24];
    char val[1024];
    std::ifstream cfg(filename.c_str());
	std::string fulltag;

    while( !cfg.eof() ){
        _ch = cfg.get();
        // printf("[%c]\n",_ch);
        if(_ch != '#' && _ch != '\n'){
            cfg.getline(_buff,1024);
            //puts(_buff);
            sscanf(_buff,"%s %*s %s",tag,&val);
            // printf("Tag: [%c]%s Value: %f\n",_ch,tag,val);
			fulltag.assign(1, _ch);
			fulltag.append(tag);
			this->m_config[fulltag] = val;
			printf("[config] %s = %s\n", fulltag.c_str(), val);
        }
        cfg.ignore(1024,'\n');
        _ch = cfg.peek();
        while(_ch==' ' && _ch=='\n'){
            cfg.ignore(1024,'\n');
            _ch = cfg.peek();
        }
    }
    cfg.close();

}

kore::ConfigFile::~ConfigFile()
{
	printf("[config] Unloading %s...\n", this->m_configfile.c_str());

}

